home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / machserver / 1.098 / lfs / lfsMem.c < prev    next >
C/C++ Source or Header  |  1990-12-07  |  6KB  |  225 lines

  1. /* 
  2.  * LfsMem.c --
  3.  *
  4.  *    Manage the memory used for segment layout and cleaning for the
  5.  *    lfs file systems
  6.  *
  7.  * Copyright 1990 Regents of the University of California
  8.  * Permission to use, copy, modify, and distribute this
  9.  * software and its documentation for any purpose and without
  10.  * fee is hereby granted, provided that the above copyright
  11.  * notice appear in all copies.  The University of California
  12.  * makes no representations about the suitability of this
  13.  * software for any purpose.  It is provided "as is" without
  14.  * express or implied warranty.
  15.  */
  16.  
  17. #ifndef lint
  18. static char rcsid[] = "$Header: /sprite/src/kernel/lfs/RCS/lfsMem.c,v 1.2 90/12/07 10:08:09 mendel Exp $ SPRITE (Berkeley)";
  19. #endif /* not lint */
  20.  
  21. #include <fs.h>
  22. #include <fsconsist.h>
  23. #include <lfsInt.h>
  24. #include <sync.h>
  25.  
  26. static Sync_Lock lfsMemLock = Sync_LockInitStatic("LfsMemLock");
  27. #define    LOCKPTR (&lfsMemLock)
  28.  
  29. static Boolean memInUse = FALSE;   /* TRUE if the resources are in uses.
  30.                     * FALSE otherwise. */
  31.  
  32. Sync_Condition    memWait;    /* Condition to wait for  
  33.                  * memory to become available. */
  34.  
  35. static int numFileSystems = 0;         /* Number of file system sharing the
  36.                       * memory. */
  37. static int maxNumCacheBlocksRes = 0; /* Number of cache blocks reserved by this
  38.                       * module for cleaning. */
  39.  
  40. /*
  41.  * Cleaning memory variables.
  42.  */
  43.  
  44. static char *cleaningMemPtr = (char *) NIL; /* Pointer to allocated memory
  45.                          * for reading in segments to 
  46.                          * clean.  */
  47. static int  cleaningMemSize = 0;    /* Size of the current allocated 
  48.                      * cleaning memory. */
  49.  
  50. /*
  51.  *----------------------------------------------------------------------
  52.  *
  53.  * LfsMemInit --
  54.  *
  55.  *    Initialize the memory resources needed by an LFS file system.
  56.  *    This routine should be called a file system attach time to 
  57.  *    insure enough memory and cache blocks exist for the file system
  58.  *    to perform segment writes and cleans.
  59.  *
  60.  * Results:
  61.  *    None.
  62.  *
  63.  * Side effects:
  64.  *    File cache blocks may be reserved.
  65.  *
  66.  *----------------------------------------------------------------------
  67.  */
  68.  
  69. void
  70. LfsMemInit(lfsPtr)
  71.     Lfs    *lfsPtr;    /* File system to initial memory for. */
  72. {
  73.     int    segSizeInBlocks, numReserved, segSize;
  74.  
  75.     LOCK_MONITOR;
  76.     segSize = LfsSegSize(lfsPtr);
  77.     segSizeInBlocks = segSize/FS_BLOCK_SIZE;
  78.  
  79.  
  80.     numReserved = 0;
  81.     /*
  82.      * Reserve the blocks we need for cleaning.   We need only add to the
  83.      * number of reserved blocks.
  84.      */
  85.     if (maxNumCacheBlocksRes < lfsPtr->superBlock.hdr.maxNumCacheBlocks) {
  86.     numReserved = lfsPtr->superBlock.hdr.maxNumCacheBlocks - 
  87.                     maxNumCacheBlocksRes;
  88.     }
  89.     numReserved = Fscache_ReserveBlocks(lfsPtr->domainPtr->backendPtr, 
  90.                 numReserved, 0);
  91.     maxNumCacheBlocksRes += numReserved;
  92.     /*
  93.      * Reserve a segment worth of cache blocks for writing and insure
  94.      * that two segments worth of cache blocks exist to act as a
  95.      * write buffer for the file system.
  96.      */
  97.     lfsPtr->mem.cacheBlocksReserved = Fscache_ReserveBlocks(
  98.                 lfsPtr->domainPtr->backendPtr, 
  99.                 segSizeInBlocks, 2*segSizeInBlocks);
  100.  
  101.     /*
  102.      * If the previously allocated cleaning memory is not large enough,
  103.      * free it and malloc one of the correct size. Besure to wait until
  104.      * the memory is not in use. 
  105.      */
  106.     if (segSize > cleaningMemSize) {
  107.     while(memInUse) {
  108.         Sync_Wait(&memWait, FALSE);
  109.     }
  110.     if (cleaningMemSize > 0) {
  111.         free(cleaningMemPtr);
  112.     }
  113.     cleaningMemSize = segSize;
  114.     cleaningMemPtr = malloc(cleaningMemSize);
  115.     }
  116.     numFileSystems++;
  117.     UNLOCK_MONITOR;
  118. }
  119.  
  120. /*
  121.  *----------------------------------------------------------------------
  122.  *
  123.  * LfsMemDetach --
  124.  *
  125.  *    Release the memory and cache resources reserved by a LFS file
  126.  *    system. This routine should be called at file system detach 
  127.  *    time.  
  128.  *
  129.  * Results:
  130.  *    None.
  131.  *
  132.  * Side effects:
  133.  *    Reserved cache blocks and memory may be released.
  134.  *
  135.  *----------------------------------------------------------------------
  136.  */
  137.  
  138. void
  139. LfsMemDetach(lfsPtr)
  140.     Lfs    *lfsPtr;    /* File system to release memory for. */
  141. {
  142.     LOCK_MONITOR;
  143.     Fscache_ReleaseReserveBlocks(lfsPtr->domainPtr->backendPtr,
  144.                     lfsPtr->mem.cacheBlocksReserved);
  145.     numFileSystems--;
  146.     /*
  147.      * If this is the last file system, release reserved blocks.
  148.      */
  149.     if  (numFileSystems == 0) {
  150.     Fscache_ReleaseReserveBlocks(lfsPtr->domainPtr->backendPtr,
  151.                     maxNumCacheBlocksRes);
  152.     maxNumCacheBlocksRes = 0;
  153.     free(cleaningMemPtr);
  154.     cleaningMemSize = 0;
  155.     }
  156.     UNLOCK_MONITOR;
  157. }
  158.  
  159. /*
  160.  *----------------------------------------------------------------------
  161.  *
  162.  * LfsMemReserve --
  163.  *
  164.  *    Reserved cache blocks and memory for a file system to perform
  165.  *    cleaning.  
  166.  *
  167.  * Results:
  168.  *    None.
  169.  *
  170.  * Side effects:
  171.  *    Memory and cache blocks reserved.  May wait for resources to
  172.  *    become available.
  173.  *
  174.  *----------------------------------------------------------------------
  175.  */
  176.  
  177. void
  178. LfsMemReserve(lfsPtr, cacheBlocksPtr, memPtrPtr)
  179.     Lfs    *lfsPtr;    /* File system to reserve memory. */
  180.     int     *cacheBlocksPtr; /* OUT: Number of cache blocks reserved. */
  181.     char **memPtrPtr;   /* OUT:  Memory for segment data. */
  182. {
  183.     LOCK_MONITOR;
  184.     /*
  185.      * First wait for the segment memory to become available.
  186.      */
  187.     while(memInUse) {
  188.     Sync_Wait(&memWait, FALSE);
  189.     }
  190.     memInUse = TRUE;
  191.     *cacheBlocksPtr = maxNumCacheBlocksRes;
  192.     *memPtrPtr = cleaningMemPtr;
  193.  
  194.     UNLOCK_MONITOR;
  195. }
  196.  
  197. /*
  198.  *----------------------------------------------------------------------
  199.  *
  200.  * LfsMemRelease --
  201.  *
  202.  *    Release the cache blocks and memory previous reserved by 
  203.  *    LfsMemReserve
  204.  *
  205.  * Results:
  206.  *    None.
  207.  *
  208.  * Side effects:
  209.  *    Memory and cache blocks released.  
  210.  *
  211.  *----------------------------------------------------------------------
  212.  */
  213.  
  214. void
  215. LfsMemRelease(lfsPtr, cacheBlocks, memPtr)
  216.     Lfs    *lfsPtr;     /* File system to release memory resources. */
  217.     int    cacheBlocks;    /* Number of cache blocks to release. */
  218.     char *memPtr;    /* Memory allocate for segment data. */
  219. {
  220.     LOCK_MONITOR;
  221.     memInUse = FALSE;
  222.     Sync_Broadcast(&memWait);
  223.     UNLOCK_MONITOR;
  224. }
  225.